import config from './config'
import store from '@/vuex/store'
let Type = {
    default: "application/x-www-form-urlencoded",
    formType: "application/json",
}
const http = {
    get(url, data, contenttype, isShowLoading) { //contenttype:请求头的类型 isShowLoading:是否显示loading
        if (isShowLoading || isShowLoading === undefined) {
            // wx.showLoading({ //数据请求前loading，
            //     title: '',
            // })
        }
        return new Promise((resolve, reject) => {
            let dataType;
            if (contenttype) {
                dataType = Type.formType
            } else {
                dataType = Type.default
            }
            // let b = wx.getStorageSync('set_cookie') == null ? null : wx.getStorageSync('set_cookie')
            let b = wx.getStorageSync('set_cookie')
            wx.request({
                url: config.host + url,
                data: data,
                method: 'GET',
                header: { // 设置请求的 header
                    "content-type": dataType,
                    // "Cookie": store.state.user.setCookie,
                    "Cookie": b,
                },
                success: (res) => {
                    // wx.hideLoading();

                    // var self = this
                    // // console.log(res.header["Set-Cookie"])
                    // if(res.header["Set-Cookie"]){
                    //     // let cookie = self.getCookie(res.header["Set-Cookie"]) 
                    //     // self.$store.dispatch("set_cookie",res.header["Set-Cookie"])
                    //     store.state.user.setCookie = res.header["Set-Cookie"]
                    // }
                    if (res.statusCode != 200) {
                        // wx.showToast({
                        //     title: "网络出错，稍后再试",
                        //     title: '',
                        //     icon: "none"
                        // });
                        return false;
                    }
                    resolve(res);
                },
                fail: (error) => {
                    // fail
                    //wx.hideLoading();
                    reject(error); //请求失败
                },
                complete: () => {
                    // wx.hideLoading();
                    // complete
                }
            })
        })
    },
    post(url, data, contenttype, isShowLoading) {
        if (isShowLoading || isShowLoading === undefined) {
            // wx.showLoading({ //数据请求前loading，
            //     title: '',
            // })
        }
        return new Promise((resolve, reject) => {
            let dataType;
            if (contenttype) {
                dataType = Type.formType
            } else {
                dataType = Type.default
            }
            let b = wx.getStorageSync('set_cookie')
            wx.request({
                url: config.host + url,
                data: data,
                method: 'POST',
                header: {
                    "content-type": dataType,
                    "Cookie": b,
                },
                success: (res) => { // success
                    //wx.hideLoading();

                    // var self = this
                    // // console.log(res.header["Set-Cookie"])
                    // if(res.header["Set-Cookie"]){
                    //     console.log('cookie值',res.header["Set-Cookie"])
                    //     // let cookie = self.getCookie(res.header["Set-Cookie"]) 
                    //     // self.$store.dispatch("set_cookie",res.header["Set-Cookie"])
                    //     store.state.user.setCookie = res.header["Set-Cookie"]
                    // }
                    resolve(res);
                },
                fail: (error) => {
                    // fail
                    //wx.hideLoading();
                    reject(error);
                },
                complete: () => {
                    // complete
                    // wx.hideLoading();
                }
            })
        })
    },
    delete(url, data, contenttype, isShowLoading) {
        if (isShowLoading || isShowLoading === undefined) {
            // wx.showLoading({ //数据请求前loading，
            //     title: '',
            // })
        }
        return new Promise((resolve, reject) => {
            let dataType;
            if (contenttype) {
                dataType = Type.formType
            } else {
                dataType = Type.default
            }
            let b = wx.getStorageSync('set_cookie')
            wx.request({
                url: config.host + url,
                data: data,
                method: 'DELETE',
                header: {
                    "content-type": dataType,
                    "Cookie": b,
                },
                success: (res) => { // success
                    //wx.hideLoading();

                    // var self = this
                    // // console.log(res.header["Set-Cookie"])
                    // if(res.header["Set-Cookie"]){
                    //     // let cookie = self.getCookie(res.header["Set-Cookie"]) 
                    //     // self.$store.dispatch("set_cookie",res.header["Set-Cookie"])
                    //     store.state.user.setCookie = res.header["Set-Cookie"]
                    // }
                    resolve(res);
                },
                fail: (error) => {
                    // fail
                    //wx.hideLoading();
                    reject(error);
                },
                complete: () => {
                    // complete
                    //wx.hideLoading();
                }
            })
        })
    }
}

export default http; //暴露出来供其他文件引用